package com.ruoyi.web.controller.an;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.an.domain.AnWechatUser;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.an.domain.CampLog;
import com.ruoyi.an.service.ICampLogService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 训练营日志Controller
 * 
 * @author wolong
 * @date 2023-04-04
 */
@RestController
@RequestMapping("/an/camplog")
public class CampLogController extends BaseController
{
    @Autowired
    private ICampLogService campLogService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    /**
     * 查询训练营日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:camp:list')")
    @GetMapping("/list")
    public TableDataInfo list(CampLog campLog)
    {
        startPage();
        List<CampLog> list = campLogService.selectCampLogList(campLog);
        return getDataTable(list);
    }

    /**
     * 导出训练营日志列表
     */
    @PreAuthorize("@ss.hasPermi('system:camp:export')")
    @Log(title = "训练营日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CampLog campLog)
    {
        List<CampLog> list = campLogService.selectCampLogList(campLog);
        ExcelUtil<CampLog> util = new ExcelUtil<CampLog>(CampLog.class);
        util.exportExcel(response, list, "训练营日志数据");
    }

    /**
     * 获取训练营日志详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:camp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(campLogService.selectCampLogById(id));
    }

    /**
     * 新增训练营日志
     */
//    @PreAuthorize("@ss.hasPermi('system:camp:add')")
//    @Log(title = "训练营日志", businessType = BusinessType.INSERT)
    @Anonymous
    @GetMapping("/addCamp")
    public AjaxResult add(HttpServletRequest request)
    {
        AnWechatUser anWechatUser = getAnWechatUser(request);
        if (anWechatUser != null)
        {
            CampLog campLog = new CampLog();
            campLog.setUserName(anWechatUser.getUserName());
            campLog.setWechatUserId(anWechatUser.getWechatUserId());
            campLog.setCreateTime(DateUtils.getNowDate());
            return AjaxResult.success(campLogService.insertCampLog(campLog));
        }
        return AjaxResult.error("失败！");
    }

    /**
     * 修改训练营日志
     */
    @PreAuthorize("@ss.hasPermi('system:camp:edit')")
    @Log(title = "训练营日志", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CampLog campLog)
    {
        return toAjax(campLogService.updateCampLog(campLog));
    }

    /**
     * 删除训练营日志
     */
    @PreAuthorize("@ss.hasPermi('system:camp:remove')")
    @Log(title = "训练营日志", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(campLogService.deleteCampLogByIds(ids));
    }


    private AnWechatUser getAnWechatUser(HttpServletRequest request) {
        String token = request.getParameter("token");
        if (StringUtils.isEmpty(token))
        {
            return null;
        }
        String string = redisTemplate.opsForValue().get("user:" + token);
        JSONObject object = JSONObject.parseObject(string);
        AnWechatUser anWechatUser = object.toJavaObject(AnWechatUser.class);
        return anWechatUser;
    }
}
